<?php

namespace App\Model\Order;

use App\Core\BaseModel;
use App\Core\Exception\DefaultException;
use App\Model\Member\MemberModel;
use App\Model\Member\MemberScoreModel;
use App\Util\UserIdentityHelper;
use EasySwoole\Component\Di;
use App\Model\Member\VipModel;
use EasySwoole\Redis\Redis;
/**
 * CREATE TABLE `ss_order` (
 * `id` char(24) NOT NULL COMMENT '订单id',
 * `task_id` int(11) NOT NULL DEFAULT '0' COMMENT '下单任务id',
 * `goods_id` int(11) NOT NULL COMMENT '商品id',
 * `goods_name` varchar(255) NOT NULL COMMENT '商品名称',
 * `goods_img` varchar(255) NOT NULL COMMENT '商品图片',
 * `account_no` char(20) NOT NULL COMMENT '刷单帐号',
 * `user_id` int(11) NOT NULL COMMENT '用户id',
 * `tracking_no` varchar(50) NOT NULL DEFAULT '' COMMENT '物流单号',
 * `order_amount` decimal(10,2) NOT NULL COMMENT '订单实付金额',
 * `service_fee` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '手续费',
 * `insurance_fee` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '保险赔付',
 * `total_profit` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '订单总提成',
 * `user_profit` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '当前用户提成',
 * `referrer_profit` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '团队提成',
 * `profit_chain` varchar(1000) DEFAULT NULL COMMENT '推荐链分级提成',
 * `order_state` char(10) NOT NULL DEFAULT 'UNSHIPPING' COMMENT '订单状态 UNSHIPPING:待发货;SHIPPED:已发货;RECEIPT:确认收货;AUDITING:退货审核中;AUDITED:退货审核通过;AFTERSALES:退货售后;RETURNED:已退货',
 * `order_time` datetime DEFAULT NULL COMMENT '下单时间',
 * `shipped_time` datetime DEFAULT NULL COMMENT '发货时间',
 * `receipt_time` datetime DEFAULT NULL COMMENT '收货时间',
 * `auditing_time` datetime DEFAULT NULL COMMENT '发起退货时间',
 * `audited_time` datetime DEFAULT NULL COMMENT '退货审核通过时间',
 * `aftersales_time` datetime NOT NULL COMMENT '退货售后时间',
 * `returned_time` datetime DEFAULT NULL COMMENT '已退货时间',
 * `created_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT '创建时间',
 * `created_by` varchar(32) NOT NULL DEFAULT '' COMMENT '创建人员账号',
 * `updated_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' ON UPDATE CURRENT_TIMESTAMP COMMENT '最后更新时间',
 * `updated_by` varchar(32) NOT NULL DEFAULT '' COMMENT '最后更新人员账号',
 * `is_delete` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除[0:未删除，1：已删除]',
 * `deleted_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT '删除时间',
 * PRIMARY KEY (`order_id`,`aftersales_time`) USING BTREE
 * ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单';
*/
/**
 * Class OrderModel
 * @property int id
 * @property int task_id
 * @property int user_id
 * @property int goods_id
 * @property string goods_name
 * @property string goods_img
 * @property string account_no
 * @property string tracking_no
 * @property float order_amount
 * @property float service_fee
 * @property float insurance_fee
 * @property float total_profit
 * @property float user_profit
 * @property float referrer_profit
 * @property string profit_chain
 * @property string order_state
 * @property string order_time
 * @property string shipped_time
 * @property string receipt_time
 * @property string auditing_time
 * @property string audited_time
 * @property string returned_time
 * @property string created_at
 */
class OrderReturnedModel extends BaseModel
{

    protected $tableName = 'ss_order_returned';

    protected $casts = [
        'id'            => 'integer',
        'task_id'       => 'integer',
        'user_id'       => 'integer',
        'goods_id'      => 'integer',
        'order_amount'  => 'float',
        'service_fee'   => 'float',
        'insurance_fee' => 'float',
        'total_profit'  => 'float',
        'user_profit'   => 'float',
        'referrer_profit' => 'float',
        'profit_chain'  => 'string',
        'order_state'   => 'string',
        'order_time'    => 'string',
        'shipped_time'  => 'string',
        'receipt_time'  => 'string',
        'auditing_time' => 'string',
        'audited_time'  => 'string',
        'returned_time' => 'string',
        'created_at'    => 'string',
    ];
    //订单状态 UNSHIPPING:待发货;SHIPPED:已发货;RECEIPT:确认收货;AUDITING:退货审核中;AUDITED:退货审核通过;AFTERSALES:退货售后;RETURNED:已退货
    const UNSHIPPING = 'UNSHIPPING';//待发货
    const SHIPPED    = 'SHIPPED';//已发货
    const RECEIPT    = 'RECEIPT';//确认收货
    const AUDITING   = 'AUDITING';//退货审核中
    const AUDITED    = 'AUDITED';//退货审核通过
    const AFTERSALES = 'AFTERSALES';//退货售后
    const RETURNED   = 'RETURNED';//已退货

    /**
     * 下单后X小时
     */
    const SHIPPEDTIME = [12, 28]; //发货时间
    const RECEIPTTIME = [72, 120]; //收货时间
    const AUDITINGTIME = [122, 132]; //发起退货时间
    const AUDITEDTIME = [133, 144]; //退货审核通过时间
    const AFTERSALESTIME = [145, 156]; //退货售后时间
    const RETURNEDTIME = [156, 168]; //已退货时间

    const AMOUNT = 28.5; //订单平均金额,用于检测下单时余额是否足够
    const AMOUNTRANGE = [20, 50]; //订单金额区间

    const INSURANCERANGE = [5, 8]; //保费区间
    const PROFITRANGE = [1.5, 1.5]; //默认提成
    const MAXPROFIT = 2.5;//提成最大值
    const MAXREFERRERPROFIT = 2.5;//下级提成最大值
    const ORDERLIMIT = 100;//订单限制默认提成
    const MAXPLOCKEDDAY = 100;//提成最大不可提现天数
    const ORDERLIMITTIME = 10;//下单时间间隔

    public static function getOrderStateDict($key = null)
    {
        return self::getMap([
            self::UNSHIPPING => '待发货',
            self::SHIPPED    => '已发货',
            self::RECEIPT    => '确认收货',
            self::AUDITING   => '退货审核中',
            self::AUDITED    => '退货审核通过',
            self::AFTERSALES => '退货售后',
            self::RETURNED   => '已退货',
        ], $key);
    }

    public function member()
    {
        return $this->hasOne(MemberModel::class, function ($query) {
            $query->where('is_delete', 0);
            return $query;
        }, 'user_id', 'id');
    }
    public function shipping()
    {
        return $this->hasOne(MemberModel::class, function ($query) {
            $query->where('is_delete', 0);
            return $query;
        }, 'tracking_no', 'shipping_id');
    }
    public function memberScore()
    {
        return $this->hasMany(MemberScoreModel::class, function ($query) {
            $query->where('is_delete', 0);
            return $query;
        }, 'user_id', 'id');
    }
    protected static function onAfterUpdate($model, $res)
    {
        self::delCache('orderCache:');
    }

    protected static function onAfterInsert($model, $res)
    {
        self::delCache('orderCache:');
    }

}
